﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DTO;
namespace DAO
{
   public  class QuyenDAO
    {
        public static Quyen GetByID(int maID)
        {
            Quyen loai = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_Quyen_GetById", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maQuyen", maID));
            SqlDataReader dataReader = cmd.ExecuteReader();
            if (dataReader.Read())
            {
                loai = new Quyen();
                loai.MaQuyen = maID;
                loai.TenQuyen = Convert.ToString(dataReader["TenQuyen"]);
                loai.MoTa = Convert.ToString(dataReader["MoTa"]);
                
                loai.DelFlag = Convert.ToInt32(dataReader["DelFlag"]);
            }
            cn.Close();
            return loai;
        }

        public static List<Quyen> GetALL()
        {
            List<Quyen> lst = new List<Quyen>();
            Quyen loai = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_Quyen_GetByMaTT", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dataReader = cmd.ExecuteReader();
            while (dataReader.Read())
            {

                loai = new Quyen();
                loai.MaQuyen = Convert.ToInt32(dataReader["@MaQuyen"]);
                loai.TenQuyen = Convert.ToString(dataReader["TenQuyen"]);
                loai.MoTa = Convert.ToString(dataReader["MoTa"]);
                loai.DelFlag = Convert.ToInt32(dataReader["DelFlag"]);
                lst.Add(loai);
            }
            cn.Close();
            return lst;
        }
        public static int GetByTen(string tenQuyen)
        {
            Quyen loai = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_Quyen_GetByTen", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@TenQuyen",tenQuyen ));
            SqlDataReader dataReader = cmd.ExecuteReader();
            if (dataReader.Read())
            {
                loai = new Quyen();
                loai.MaQuyen = Convert.ToInt32(dataReader["MaQuyen"]);
                loai.TenQuyen = tenQuyen;
                loai.MoTa = Convert.ToString(dataReader["MoTa"]);

                loai.DelFlag = Convert.ToInt32(dataReader["DelFlag"]);
            }
            cn.Close();
            return loai.MaQuyen;
        }
    }
}
